import { useUserCenter } from "~/pinia/useUserCenter.js";

const hasPermission = (value, el = false) => {
  const user = useUserCenter();
  if (!Array.isArray(value)) {
    throw new Error("v-auth need array");
  }
  const hasAuth = value.findIndex((v) => user.roleNames.includes(v)) != -1;
  if (el && !hasAuth) {
    el.parentNode && el.parentNode.removeChild(el);
  }
  return hasAuth;
};

export default {
  install(app) {
    app.directive("auth", {
      mounted(el, bindding) {
        hasPermission(bindding.value, el);
      },
    });
  },
};
